﻿/****************************** Module Header ******************************\
 * Module Name:  _Request.cs
 * Module Version:  1.0.0 (2012-9-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for _Request.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UCL.Web
{
    /// <summary>
    /// 私有请求类
    /// </summary>
    /// <remarks>
    /// 2012-9-16 1.0.0 by sutroon
    /// 2014-4-2 1.5.0 by sutroon 重构代码,加入defv和queryType特性,删除url和form专用方法.
    /// </remarks>
    public class _Request : HttpSessionStateBase
    {
        public static HttpRequest Request { get { return HttpContext.Current.Request; } }

        public static bool HasVar(string name, string queryType = "ALL")
        {
            if ((queryType == "ALL" || queryType == "URL") && Request.QueryString.AllKeys.Contains(name)) return true;
            if ((queryType == "ALL" || queryType == "FORM") && Request.Form.AllKeys.Contains(name)) return true;
            if ((queryType == "ALL" || queryType == "COOKIE") && Request.Cookies.AllKeys.Contains(name)) return true;
            if ((queryType == "ALL" || queryType == "COOKIE") && Request.Cookies.AllKeys.Contains(name)) return true;
            if ((queryType == "ALL" || queryType == "SESSION") && HttpContext.Current.Session[name] != null) return true;
            return false;
        }

        public static string GetString(string name, string defv = "", string queryType = "ALL")
        {
            if ((queryType == "ALL" || queryType == "URL") && Request.QueryString.AllKeys.Contains(name))
            {
                return Request.QueryString[name];
            }
            if ((queryType == "ALL" || queryType == "FORM") && Request.Form.AllKeys.Contains(name))
            {
                return Request.Form[name];
            }
            if ((queryType == "ALL" || queryType == "COOKIE") && Request.Cookies.AllKeys.Contains(name))
            {
                return Request.Cookies[name].Value;
            }

            if ((queryType == "ALL" || queryType == "SESSION") && HttpContext.Current.Session[name] != null)
            {
                return HttpContext.Current.Session[name].ToString();
            }
            return defv;
        }
        public static bool GetBool(string name, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToBoolean();
        }
        public static int GetInt(string name, int defv = 0, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToInt(defv);
        }
        public static decimal GetDecimal(string name, decimal defv = 0, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToDecimal(defv);
        }
        public static short GetShort(string name, short defv = 0, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToShort(defv);
        }
        public static double GetDouble(string name, double defv = 0, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToDouble(defv);
        }
        public static Guid GetGuid(string name, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToGuid();
        }
        public static DateTime GetDateTime(string name, string queryType = "")
        {
            return GetString(name, queryType: queryType).ToDateTime();
        }
    }
}
